#!/bin/bash
set -e

TMP=${TMP:-/tmp/work}
LOG=${LOG:-$HOME/logs/stage1}
SRC=${SRC:-$LFS/sources}

name=gcc-pass2
version=10.2.0
mpfr_ver=4.1.0
gmp_ver=6.2.1
mpc_ver=1.2.1

rm -fr $TMP
mkdir -pv $TMP $LOG

tar xf $SRC/gcc-$version.tar.xz -C $TMP 

{ time \
   {
    
    cd $TMP/gcc-$version

    tar -xf $SRC/mpfr-$mpfr_ver.tar.xz
    tar -xf $SRC/gmp-$gmp_ver.tar.xz
    tar -xf $SRC/mpc-$mpc_ver.tar.gz
    
    mv -v mpfr-$mpfr_ver mpfr
    mv -v gmp-$gmp_ver gmp
    mv -v mpc-$mpc_ver mpc

    case $(uname -m) in
    x86_64)
        sed -e '/m64=/s/lib64/lib/' -i.orig gcc/config/i386/t-linux64
    ;;
    esac

    mkdir -v build
    cd       build

    mkdir -pv $LFS_TGT/libgcc
    ln -s ../../../libgcc/gthr-posix.h $LFS_TGT/libgcc/gthr-default.h

    ../configure                                       \
        --build=$(../config.guess)                     \
        --host=$LFS_TGT                                \
        --prefix=/usr                                  \
        CC_FOR_TARGET=$LFS_TGT-gcc                     \
        --with-build-sysroot=$LFS                      \
        --enable-initfini-array                        \
        --disable-nls                                  \
        --disable-multilib                             \
        --disable-decimal-float                        \
        --disable-libatomic                            \
        --disable-libgomp                              \
        --disable-libquadmath                          \
        --disable-libssp                               \
        --disable-libvtv                               \
        --disable-libstdcxx                            \
        --enable-languages=c,c++

    make

    make DESTDIR=$LFS install

    ln -sv gcc $LFS/usr/bin/cc

    }
} 2>&1 | tee $name.log

[ $PIPESTATUS = 0 ] && mv $name.log $LOG || exit $PIPESTATUS

rm -fr $TMP
